define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tools/tools/index' + location.search,
                    add_url: 'tools/tools/add',
                    edit_url: 'tools/tools/edit',
                    del_url: 'tools/tools/del',
                    multi_url: 'tools/tools/multi',
                    import_url: 'tools/tools/import',
                    table: 'tools',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'images', title: __('Images'), operate: false, formatter: Controller.api.formatter.thumb},
                        {field: 'view_num', title: __('View_num')},
                        {field: 'use_num', title: __('Use_num')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),'hidden':__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#faupload-images").data("upload-success", function(btn,data, ret){
                // var button = btn.element;
                // var data = typeof data.data !== 'undefined' ? data.data : null;
                //上传成功后回调
                // if (button) {
                //     //如果有文本框则填充
                //     var input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
                //     console.log(input_id);
                //     return false;
                //     if (input_id) {
                //         var urlArr = [];
                //         var inputObj = $("#" + input_id);
                //         if ($(button).data("multiple") && inputObj.val() !== "") {
                //             urlArr.push(inputObj.val());
                //         }
                //         var url = Config.upload.fullmode ? Fast.api.cdnurl(data.url) : data.fullurl;
                //         urlArr.push(url);
                //         inputObj.val(urlArr.join(",")).trigger("change").trigger("validate");
                //     }
                //     //如果有回调函数
                //     var onDomUploadSuccess = $(button).data("upload-success");
                //     if (onDomUploadSuccess) {
                //         if (typeof onDomUploadSuccess !== 'function' && typeof Upload.api.custom[onDomUploadSuccess] === 'function') {
                //             onDomUploadSuccess = Upload.api.custom[onDomUploadSuccess];
                //         }
                //         if (typeof onDomUploadSuccess === 'function') {
                //             var result = onDomUploadSuccess.call(button, data, ret);
                //             if (result === false)
                //                 return;
                //         }
                //     }
                // }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    var html = '';
                        html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + '" alt="" style="max-height:60px;max-width:120px"></a>';

                    return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                },
            }
        }
    };
    return Controller;
});
